module QuestionnaireManager
    
  def self.create_questionnaires(data_collection)
    data_collection_types = DataCollectionType.all_types

    case data_collection.data_collection_type.name
    when data_collection_types[:PRE_TREATMENT]
      create_questionnaires_for_pre_treatment(data_collection.id)
    when data_collection_types[:FEQ]
      create_questionnaire(data_collection.id, QuestionnaireName.all_names[:FEQ])
    when data_collection_types[:SCAPI1]
      create_questionnaire(data_collection.id, QuestionnaireName.all_names[:SCAPI1])
    when data_collection_types[:SCAPI2]
      create_questionnaire(data_collection.id, QuestionnaireName.all_names[:SCAPI2])
    when data_collection_types[:SCAPI3]
      create_questionnaire(data_collection.id, QuestionnaireName.all_names[:SCAPI3])
    when data_collection_types[:SCAPI4]
      create_questionnaire(data_collection.id, QuestionnaireName.all_names[:SCAPI4])
    when data_collection_types[:SIX_MONTHS_FOLLOW_UP]
      create_questionnaires_for_six_months_follow_up(data_collection.id)
    when data_collection_types[:ONE_YEAR_FOLLOW_UP]
      create_questionnaires_for_one_year_follow_up(data_collection.id)
    when data_collection_types[:TWO_YEAR_FOLLOW_UP]
      create_questionnaires_for_two_year_follow_up(data_collection.id)      
    end
  end

  

  private

  def self.create_questionnaire(data_collection_id, name)
    raise "Arjas vrede" if name.nil?
    raise "Arjas vrede 2: #{name}" if QuestionnaireName.find_by_name(name).nil?

    @questionnaire = Questionnaire.new(:data_collection_id => data_collection_id,
      :questionnaire_name_id => QuestionnaireName.find_by_name(name).id)

    @questionnaire.save
  end


  def self.create_questionnaires_for_pre_treatment(data_collection_id)
    create_standard_adolescent_questionnaires(data_collection_id)
    create_standard_parent_questionnaires(data_collection_id)
    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:TRF])
  end


  def self.create_questionnaires_for_six_months_follow_up(data_collection_id)
    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:YSR])
    create_standard_parent_questionnaires(data_collection_id)
  end


  def self.create_questionnaires_for_one_year_follow_up(data_collection_id)
    create_standard_adolescent_questionnaires(data_collection_id)
    create_standard_parent_questionnaires(data_collection_id)
  end


  def self.create_questionnaires_for_two_year_follow_up(data_collection_id)
    create_standard_adolescent_questionnaires(data_collection_id)
    create_standard_parent_questionnaires(data_collection_id)
    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:FSS])
    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:TRF])

  end


  def self.create_standard_adolescent_questionnaires(data_collection_id)
    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:YLS])
    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:EURO_ADAD])
    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:YSR])
  end


#  def self.create_reduced_set_of_adolescent_questionnaires(data_collection_id)
#    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:YLS])
#    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:YSR])
#  end


  def self.create_standard_parent_questionnaires(data_collection_id)
    create_questionnaire(data_collection_id, QuestionnaireName.all_names[:CBCL])
    # create_questionnaire(data_collection_id, QuestionnaireName.all_names[:SCL_90])
  end

end
